//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.planw.beetl.utils;

import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiType;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.PsiUtil;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import org.apache.commons.lang3.ObjectUtils;
import org.intellij.plugins.markdown.lang.MarkdownElementTypes;

public class PsiKt {
  private static final List<String> primitiveClasses = new ArrayList();

  public PsiKt() {
  }

  public static boolean isPrimitive(PsiType psiType) {
    return primitiveClasses.contains(getClassName(psiType)) || primitiveClasses.contains(psiType.getCanonicalText());
  }

  public static String getClassName(PsiType psiType) {
    PsiClass psiClass = PsiUtil.resolveClassInType(psiType);
    return psiClass == null ? psiType.getCanonicalText() : (String)Optional.of(psiClass).map(PsiClass::getQualifiedName).orElse("");
  }

  public static PsiElement findParentOfType(PsiElement node, IElementType parentType) {
    while(ObjectUtils.notEqual(Optional.ofNullable(node).map(PsiElement::getNode).map(ASTNode::getElementType).orElse(null), MarkdownElementTypes.MARKDOWN_FILE)) {
      node = (PsiElement)Optional.ofNullable(node).map(PsiElement::getParent).orElse(null);
    }

    return node;
  }

  public static <T> T findChildByClass(PsiElement psiElement, Class<T> aClass) {
    for(PsiElement cur = psiElement.getFirstChild(); cur != null; cur = cur.getNextSibling()) {
      if (aClass.isInstance(cur)) {
        return (T)cur;
      }
    }

    return null;
  }

  static {
    primitiveClasses.add(Integer.TYPE.getCanonicalName());
    primitiveClasses.add(Integer.class.getCanonicalName());
    primitiveClasses.add(Double.TYPE.getCanonicalName());
    primitiveClasses.add(Double.class.getCanonicalName());
    primitiveClasses.add(Float.TYPE.getCanonicalName());
    primitiveClasses.add(Float.class.getCanonicalName());
    primitiveClasses.add(Short.TYPE.getCanonicalName());
    primitiveClasses.add(Short.class.getCanonicalName());
    primitiveClasses.add(Byte.TYPE.getCanonicalName());
    primitiveClasses.add(Byte.class.getCanonicalName());
    primitiveClasses.add(Character.TYPE.getCanonicalName());
    primitiveClasses.add(Character.class.getCanonicalName());
    primitiveClasses.add(Long.TYPE.getCanonicalName());
    primitiveClasses.add(Long.class.getCanonicalName());
    primitiveClasses.add(Boolean.TYPE.getCanonicalName());
    primitiveClasses.add(Boolean.class.getCanonicalName());
    primitiveClasses.add(String.class.getCanonicalName());
    primitiveClasses.add(Date.class.getCanonicalName());
    primitiveClasses.add(java.sql.Date.class.getCanonicalName());
    primitiveClasses.add(LocalDate.class.getCanonicalName());
    primitiveClasses.add(LocalDateTime.class.getCanonicalName());
    primitiveClasses.add(LocalTime.class.getCanonicalName());
  }
}
